/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toolz;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Bunkowski
 */
public class TrashLabel extends JLabel implements DropTargetListener{

	public static final long serialVersionUID = 1L;
	
    public JTable table;
    public DropTarget dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,this, true, null);

    public TrashLabel() {
       setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/trash.jpg")));
       setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
       setText("");
    }


    public void dragEnter(DropTargetDragEvent dtde) {
    setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    }
    public void dragOver(DropTargetDragEvent dtde) { }
    public void dropActionChanged(DropTargetDragEvent dtde) {    }
    public void dragExit(DropTargetEvent dte) {
    setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    }

    public void drop(DropTargetDropEvent evt) {

        try {
            Transferable t = evt.getTransferable();
            String tabseparatedFileList = (String) t.getTransferData(DataFlavor.stringFlavor);
            addInformationFomDrop(tabseparatedFileList);
        } catch (UnsupportedFlavorException ex) {
        } catch (IOException ex) {
        }
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
    }

    private void addInformationFomDrop(String string) {

        DefaultTableModel dtm = (DefaultTableModel)table.getModel();
        int[] row = table.getSelectedRows();

        for (int i = row.length; i > 0; i--) {
            int rowNr = table.getRowSorter().convertRowIndexToModel(row[i-1]);
            dtm.removeRow(rowNr);
        }
        
        
        //complex delete model
        /*int[] clm = table.getSelectedColumns();
        for (int i : clm) System.out.print(i+" ");
        System.out.println("");

        for (int i = row.length; i > 0; i--) {
        //int rowNr = table.getRowSorter().convertRowIndexToModel(row[i-1]);
        for (int j = clm.length; j > 0; j--) {
        int clmNr = clm[j-1];
        table.setValueAt("", row[i-1], clmNr);
        }
        }

        boolean[] emptyColumn = new boolean[dtm.getColumnCount()];
        for (int i = 0; i < emptyColumn.length; i++) emptyColumn[i] = true;

        boolean[] emptyRow = new boolean[dtm.getRowCount()];
        for (int i = 0; i < emptyRow.length; i++) emptyRow[i] = true;


        for (int j = 0; j < dtm.getRowCount(); j++) {
        for (int i = 0; i < dtm.getColumnCount(); i++) {
        //   System.out.println(j+"/"+i);
        int rowNr = table.getRowSorter().convertRowIndexToModel(j);
        int columnNr = i;
        if(table.getValueAt(rowNr,columnNr) != null){
        if (!table.getValueAt(rowNr,columnNr).equals("")){
        emptyColumn[i] = false;
        emptyRow[rowNr] = false;
        }
        }
        }
        }


        for (int i = 0; i < emptyRow.length; i++) {
        if (emptyRow[i]) dtm.removeRow(i);
        }
        for (int i = 0; i < emptyColumn.length; i++) {
        if (emptyColumn[i]){
        table.removeColumn(table.getColumnModel().getColumn(i));
        }

        }*/

    }



}
